package com.handinfo.engine;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.resource.ObjectAdapter;
import com.handinfo.engine.resource.ResHead;
import com.handinfo.engine.resource.Resource;
import com.handinfo.engine.resource.ResourceManager;
import com.handinfo.engine.resource.Serializeable;
import com.handinfo.util.Tools;

/**
 * @author Xia Mingwei
 * @version 1.0
 */
public class Actor extends Serializeable
{
	public AbstractGameScene m_scene; // 元件所在的场景，该场景管理此元件
	public short m_id; // 元件在场景中的id
	public Animation m_anim; // 主动画，暂定为m_anims[0],通过操作m_anim来达到控制所有m_anims的效果。
	public Animation[] m_anims; // 元件绑定动画（用于换装系统，将anim拆成多个anim，目前要求所有的m_anims有相同的动作以及动作内的帧信息）
	public int m_actionIDNow; // 当前元件动画所在动作索引
	public int m_actionIDBefore; // 前一个元件动画动作索引
	public int m_actionIDNext = -1; // 下一个元件动画动作索引
	public int m_frameId; // 当前元件动画所在帧编号（在动作中的编号）
	public int m_frameIndex; // 当前元件动画所在帧索引（在所有帧中的索引）
	public int m_frameDuration; // 当前元件动画帧持续帧数
	public boolean m_actionCycle = true; // 动作是否循环执行
	public boolean m_actionOver; // 动作是否执行完毕
	public int m_layer; // 层索引，不同层元件绘制顺序不同, 与其m_phylayer一定程度上统一（1-4层统一）
	public int m_phylayer; // 元件所在物理层索引 (-1：随意行走 0:无法通过 1-4：互斥，只能通往-1，9，10，11
	// 8：最高层，无视任何物理层碰撞 9-11：衔接互斥的两层)
	public int m_prePhylayer; // 先前站立的物理层索引
	public int m_flag; // 标记位
	public boolean m_flipX; // 水平翻转标记
	public boolean m_flipY; // 垂直翻转标记
	public boolean m_visible = true; // 元件是否显示状态
	public boolean m_enabled; // 元件是否有效状态
	public boolean m_needDraw = true; // 元件是否在视口范围内
	public boolean m_isOnce = false; // 元件是否在一次动作后销毁或者消失
	// public boolean m_movable = true; //元件是否可移动，如果不可移动则不会updatePos
	public short[] m_collideBox = new short[4]; // 碰撞框，主要用于物理碰撞处理
	public short[] m_activeBox = new short[4]; // 激活框，主要用于逻辑处理
	public short[] m_parameters = new short[0]; // 元件参数数组

	public Actor()
	{
	}

	public Actor(Resource resource)
	{
		m_resource = resource;
	}

	// ////////////////////////////////////////
	// 玩家与角色共有属性
	// ////////////////////////////////////////
	public long m_gameID = 0;// ID, 游戏中唯一的标识

	// public String m_name = null;//名字
	// public short m_lv = 0;//等级

	public Actor(Animation anim)
	{
		m_anims = new Animation[1];
		m_anims[0] = anim;
		m_anim = m_anims[0];
		m_enabled = false;
		m_visible = false;
	}

	public Actor(Animation[] anims)
	{
		m_anims = anims;
		m_anim = anims[0];
		m_enabled = false;
		m_visible = false;
	}

	public Actor(AbstractGameScene scene)
	{
		this.m_scene = scene;
	}

	/**
	 * 载入数据
	 * 
	 * @param dis
	 * @throws java.lang.Exception
	 */
	public void load(DataInputStream dis) throws Exception
	{
		m_actionIDNow = dis.readUnsignedByte();
		m_layer = dis.readByte();
		// m_flag = dis.readInt();
		m_flipX = (m_flag & 0x01) != 0;
		m_flipY = (m_flag & 0x02) != 0;
		m_enabled = (m_flag & 0x04) != 0;
		m_visible = (m_flag & 0x08) != 0;
		m_posX = dis.readShort();
		m_posY = dis.readShort();
		// dis.skipBytes(1);
		m_collideBox = new short[4];
		m_collideBox[0] = dis.readShort();
		m_collideBox[1] = dis.readShort();
		m_collideBox[2] = dis.readShort();
		m_collideBox[2] = (short) (m_collideBox[0] + m_collideBox[2]);
		m_collideBox[3] = dis.readShort();
		m_collideBox[3] = (short) (m_collideBox[1] + m_collideBox[3]);
		m_activeBox = new short[4];
		m_activeBox[0] = dis.readShort();
		m_activeBox[1] = dis.readShort();
		m_activeBox[2] = dis.readShort();
		m_activeBox[2] = (short) (m_activeBox[0] + m_activeBox[2]);
		m_activeBox[3] = dis.readShort();
		m_activeBox[3] = (short) (m_activeBox[1] + m_activeBox[3]);
		int temp_parameterCount = dis.readByte();
		if (temp_parameterCount != 0) {
			m_parameters = new short[temp_parameterCount];
			for (int j = 0; j < temp_parameterCount; j++) {
				m_parameters[j] = dis.readShort();
			}
		}
	}

	/**
	 * 克隆元件对象，克隆对象为自己
	 * 
	 * @return
	 */
	public Actor clone()
	{
		return clone(this);
	}

	/**
	 * 克隆给定的元件对象
	 * 
	 * @param actor
	 * @return
	 */
	public Actor clone(Actor actor)
	{
		Actor ca = new Actor();

		ca.m_scene = actor.m_scene;
		ca.m_anims = new Animation[actor.m_anims.length];
		System.arraycopy(actor.m_anims, 0, ca.m_anims, 0, actor.m_anims.length);
		ca.m_anim = actor.m_anim;
		ca.m_actionIDNow = actor.m_actionIDNow;
		ca.m_actionIDBefore = actor.m_actionIDBefore;
		ca.m_frameId = actor.m_frameId;
		ca.m_frameIndex = actor.m_frameIndex;
		ca.m_frameDuration = actor.m_frameDuration;
		ca.m_actionCycle = actor.m_actionCycle;
		ca.m_actionOver = actor.m_actionOver;
		ca.m_layer = actor.m_layer;
		ca.m_phylayer = actor.m_phylayer;
		ca.m_flag = actor.m_flag;
		ca.m_flipX = actor.m_flipX;
		ca.m_flipY = actor.m_flipY;
		ca.m_visible = actor.m_visible;
		ca.m_enabled = actor.m_enabled;
		// ca.m_movable = actor.m_movable;
		ca.m_needDraw = actor.m_needDraw;
		ca.m_posX = actor.m_posX;
		ca.m_posY = actor.m_posY;
		ca.m_vX = actor.m_vX;
		ca.m_vY = actor.m_vY;
		ca.m_collideBox = new short[actor.m_collideBox.length];
		System.arraycopy(actor.m_collideBox, 0, ca.m_collideBox, 0,
				actor.m_collideBox.length);
		ca.m_activeBox = new short[actor.m_activeBox.length];
		System.arraycopy(actor.m_activeBox, 0, ca.m_activeBox, 0,
				actor.m_activeBox.length);
		ca.m_parameters = new short[actor.m_parameters.length];
		System.arraycopy(actor.m_parameters, 0, ca.m_parameters, 0,
				actor.m_parameters.length);
		ca.m_resHead = actor.m_resHead;
		ca.m_resource = actor.m_resource;
		ca.m_refsResHead = new ResHead[actor.m_refsResHead.length];
		System.arraycopy(actor.m_refsResHead, 0, ca.m_refsResHead, 0,
				actor.m_refsResHead.length);
		return ca;
	}

	/**
	 * 根据指定Actor初始化自身部分属性
	 * 
	 * @param actor
	 */
	public void copy(Actor actor)
	{
		m_scene = actor.m_scene;
		m_anims = new Animation[actor.m_anims.length];
		System.arraycopy(actor.m_anims, 0, m_anims, 0, actor.m_anims.length);
		m_anim = actor.m_anim;
		System.arraycopy(actor.m_collideBox, 0, m_collideBox, 0,
				actor.m_collideBox.length);
		System.arraycopy(actor.m_activeBox, 0, m_activeBox, 0,
				actor.m_activeBox.length);
		m_parameters = new short[actor.m_parameters.length];
		System.arraycopy(actor.m_parameters, 0, m_parameters, 0,
				actor.m_parameters.length);
		m_velocity = actor.m_velocity;
	}

	/**
	 * 设置隐藏/显示标记
	 * 
	 * @param visible
	 * boolean
	 */
	public void setVisible(boolean visible)
	{
		this.m_visible = visible;
	}

	/**
	 * 设置有效标记
	 * 
	 * @param enabled
	 * boolean
	 */
	public void setEnabled(boolean enabled)
	{
		this.m_enabled = enabled;
	}

	/**
	 * 设置元件参数值
	 * 
	 * @param index
	 * int
	 * @param value
	 * int
	 */
	public void setParameter(int index, int value)
	{
		if (index >= m_parameters.length) {
			return;
		}
		m_parameters[index] = (short) value;
	}

	/**
	 * 获得元件参数值
	 * 
	 * @param index
	 * int
	 * @return short
	 */
	public short getParameter(int index)
	{
		if (index >= m_parameters.length) {
			return -1;
		}
		return m_parameters[index];
	}

	public void activate()
	{
		m_enabled = true;
		m_visible = true;
	}

	public void deactivate()
	{
		m_enabled = false;
		m_visible = false;
	}

	// ///////////////////////////////////////////////////////////
	// 元件动作及帧处理代码块开始……
	/**
	 * 设定元件动作
	 * 
	 * @param actionIDNext
	 * @param mustChange
	 */
	public void setAction(int actionIDNext, boolean mustChange)
	{
		if (m_anim == null) {
			if (Tools.DEBUG) {
				System.err.println("Actor ERROR: m_anim为空");
			}
			return;
		}
		if (!mustChange) {
			if (m_actionIDNow == actionIDNext) {
				return;
			}
		}
		if (m_actionIDNow != actionIDNext) {
			m_actionIDBefore = m_actionIDNow;
		}
		m_actionIDNow = actionIDNext;
		m_frameId = 0;
		try {
			if (m_actionIDNow < 0) {
				if (Tools.DEBUG) {
					System.err.println("ERROR: actionIDNow < 0 " + m_actionIDNow);
				}
			}
			else if (m_actionIDNow < m_anim.m_actions.length) {
				if (m_anim.m_actions[m_actionIDNow].length != 0) {
					m_frameIndex = m_anim.m_actions[m_actionIDNow][0] & 0x03FF;
				}
			}
			else {
				m_frameIndex = m_anim.m_actions[m_actionIDNow % m_anim.m_actions.length][0] & 0x03FF;
				if (Tools.DEBUG) {
					System.err.println("ERROR: actionIDNow >= m_anim.m_actions.length");
				}
			}
		}
		catch (Exception ex) {
			Tools.debug(ex);
		}

		m_frameDuration = 0;
		m_actionOver = false;
	}

	public void setAction(int actionIDNext, boolean actionCycle,
			boolean mustChange)
	{
		setAction(actionIDNext, mustChange);
		m_actionCycle = actionCycle;
	}

	public void setAction(int actionIDNext, int vx, int vy, boolean actionCycle, boolean mustChange)
	{
		setAction(actionIDNext, mustChange);
		if (m_vX != vx || m_vY != vy) {
			m_preVX = m_vX;
			m_preVY = m_vY;
		}
		m_vX = vx;
		m_vY = vy;
		m_actionCycle = actionCycle;
	}

	/**
	 * 将元件动作设置到指定的帧
	 * 
	 * @param frameID
	 * int
	 */
	public void setFrame(int frameID)
	{
		// 取余数从而达到循环执行动作帧的效果
		frameID %= m_anim.m_actions[m_actionIDNow].length;
		m_frameId = frameID;
		// 设置帧索引和帧持续帧数
		m_frameIndex = (m_anim.m_actions[m_actionIDNow][frameID] & 0x03FF);
		m_frameDuration = 0;
	}

	/**
	 * 设定元件动作，同时设置到特定帧
	 * 
	 * @param actionIDNext
	 * 动作ID
	 * @param frameID
	 * 帧
	 */
	public void setActionFrame(int actionIDNext, int frameID)
	{
		setAction(actionIDNext, true);
		setFrame(frameID);
	}

	/**
	 * 指向动作上一帧
	 */
	public void preFrame()
	{
		m_frameDuration--;
		try {
			if (m_anim == null
					|| m_frameDuration < ((m_anim.m_actions[m_actionIDNow][m_frameId] & 0xFFFF) >> 10)) {
				return;
			}

			m_frameId--;
			if (m_frameId < 0) {
				m_actionOver = true;
				if (m_actionCycle) {
					m_frameId = m_anim.m_actions[m_actionIDNow].length - 1;
				}
				else {
					m_frameId++;
				}
			}

			m_frameIndex = (m_anim.m_actions[m_actionIDNow][m_frameId] & 0x03FF);
			m_frameDuration = 0;
		}
		catch (Exception ex) {
			Tools.debug(ex);
		}
	}

	/**
	 * 指向动作下一帧
	 */
	public void nextFrame()
	{
		try {
			m_frameDuration++;
			if (m_anim == null
					|| m_anim.m_actions == null
					|| m_frameId >= m_anim.m_actions[m_actionIDNow].length
					|| m_frameDuration < ((m_anim.m_actions[m_actionIDNow][m_frameId] & 0xFFFF) >> 10)) {
				return;
			}
			m_frameId++;
			if (m_anim.m_actions.length > m_actionIDNow) {
				if (m_frameId >= m_anim.m_actions[m_actionIDNow].length) {
					m_actionOver = true;
					if (m_actionCycle) {
						m_frameId = 0;
					}
					else {
						m_frameId--;
					}
				}
				m_frameIndex = (m_anim.m_actions[m_actionIDNow][m_frameId] & 0x03FF);
				m_frameDuration = 0;
			}
		}
		catch (Exception ex) {
			if (m_anim != null && m_anim.m_resHead != null) {
				Tools.debugPrintln("Actor nextFrame Exception : " + m_anim.m_resHead.m_url);
			}
			Tools.debug(ex);
		}
	}

	/**
	 * 获取一个Action的时间
	 * 
	 * @return int
	 */
	public int getActionTime()
	{
		int time = 0;
		for (int i = 0; i < m_anim.m_actions[m_actionIDNow].length; i++) {
			time += ((m_anim.m_actions[m_actionIDNow][m_frameId] & 0xFFFF) >> 10);
		}
		return time;
	}

	// 元件动作及帧处理代码块结束
	// ///////////////////////////////////////////////////////////
	public void logic()
	{
//		if (m_enabled && (m_flag & ACTOR_FLAG_CG) != 0) {
//			if (collide(GameManager.getInstance().m_role, true)) {
//				m_scene.setEventActorInfo(0, m_id, m_actionIDNow);
//				m_scene.fireEvent(AbstractGameScript.EVENT_COLLIDE_PLAYER);
//			}
//		}
	}

	/**
	 * 元件绘制
	 * 
	 * @param g
	 * @param camX
	 * @param camY
	 */
	public void draw(Graphics g, int camX, int camY)
	{
		drawAnimations(g, camX, camY);
		// m_anim.draw(g, x, y, m_flipX, m_flipY, m_frameIndex);

		// //test临时绘制碰撞框
		// g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH,
		// AbstractGameScreen.SCREEN_HEIGHT);
		// g.setColor(0x00ffff);
		// if(m_collideBox != null && m_collideBox.length != 0) {
		// g.drawRect(x + m_collideBox[0], y + m_collideBox[1], m_collideBox[2]
		// - m_collideBox[0] - 1, m_collideBox[3] - m_collideBox[1] - 1);
		// }
		// g.setColor(0xff0000);
		// if(m_activeBox != null && m_activeBox.length != 0) {
		// g.drawRect(x + m_activeBox[0], y + m_activeBox[1], m_activeBox[2] -
		// m_activeBox[0] - 1, m_activeBox[3] - m_activeBox[1] - 1);
		// }
	}

	public void drawAnimations(Graphics g, int camX, int camY)
	{
		if (m_anims == null || m_anims.length == 0)
			return;
		int x = m_posX - camX;
		int y = m_posY - camY;
		try {
			for (int i = 0; i < m_anims.length; i++) {
				if (m_anims[i] != null) {
//				Tools.debugPrintln("m_anims i="+i+";ID="+this.m_gameID+"m_frameIndex : " + m_frameIndex);
					m_anims[i].draw(g, x, y, m_flipX, m_flipY, m_frameIndex);
//					m_anims[i].draw(g, x, y, m_flipX, m_flipY, m_actionIDNow, m_frameId);
				}
			}
		}
		catch (Exception ex) {
			Tools.debug(ex);
		}
	}

	public int m_posX; // X坐标
	public int m_posY; // Y坐标
	public int m_posZ; // 目前仅用于显示
	public int m_prevPosX; // 上一帧X坐标
	public int m_prevPosY; // 上一帧Y坐标
	public int m_destX; // 目的地X坐标
	public int m_destY; // 目的地Y坐标
	public static final int m_velocity_role = 8 << Actor.ACTOR_FRACTION_BIT;//人物正常移动速度
	public int m_velocity = 8 << Actor.ACTOR_FRACTION_BIT; // 速度
	public int m_vX; // X方向每帧位移
	public int m_vY; // Y方向每帧位移
	public int m_preVX; // 上次设置的速度X
	public int m_preVY; // 上次设置的速度Y

	/**
	 * 元件位置移动
	 */
	public void updatePos()
	{
		m_prevPosX = m_posX;
		m_prevPosY = m_posY;
		m_posX += (m_vX >> ACTOR_FRACTION_BIT);
		m_posY += (m_vY >> ACTOR_FRACTION_BIT);
		if (m_scene == null || m_activeBox == null) {
			m_needDraw = false;
			return;
		}
		m_needDraw = (m_posX + m_activeBox[2] >= m_scene.m_cameraX)
				&& (m_posX + m_activeBox[0] <= m_scene.m_cameraX
				+ m_scene.m_cameraW)
				&& (m_posY + m_activeBox[3] >= m_scene.m_cameraY)
				&& (m_posY + m_activeBox[1] <= m_scene.m_cameraY
				+ m_scene.m_cameraH);

	}

	/**
	 * 设置元件位置
	 * 
	 * @param x
	 * @param y
	 */
	public void setPosition(int x, int y)
	{
		m_prevPosX = m_posX;
		m_prevPosY = m_posY;
		m_posX = x;
		m_posY = y;
		m_needDraw = (m_posX + m_activeBox[2] >= m_scene.m_cameraX)
				&& (m_posX + m_activeBox[0] <= m_scene.m_cameraX
				+ m_scene.m_cameraW)
				&& (m_posY + m_activeBox[3] >= m_scene.m_cameraY)
				&& (m_posY + m_activeBox[1] <= m_scene.m_cameraY
				+ m_scene.m_cameraH);
	}

	/**
	 * 更新角色的相关层信息
	 */
	public void updateLayerInfo()
	{
		int layInx = getCurTilePhy(m_posX, m_posY);
		// 默认情况下，m_phylayer=角色站立的物理层id
		if (layInx > 0) {
			m_prePhylayer = m_phylayer;
			m_phylayer = layInx;
		}
		// //一般情况下，显示层m_layer数值在1-4之间
		// if (layInx > 0 && layInx < 5) {
		// m_layer = layInx;
		// }
	}

	/**
	 * 恢复元件位置
	 */
	public void restorePos()
	{
		m_posX = m_prevPosX;
		m_posY = m_prevPosY;
		m_needDraw = (m_posX + m_activeBox[2] >= m_scene.m_cameraX)
				&& (m_posX + m_activeBox[0] <= m_scene.m_cameraX
				+ m_scene.m_cameraW)
				&& (m_posY + m_activeBox[3] >= m_scene.m_cameraY)
				&& (m_posY + m_activeBox[1] <= m_scene.m_cameraY
				+ m_scene.m_cameraH);
	}

	/***
	 * 判断是否与指定的元件碰撞
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * 是否忽略元件层
	 * @return
	 */
	public boolean collide(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x = m_posX + m_collideBox[0];
		int y = m_posY + m_collideBox[1];
		int w = m_posX + m_collideBox[2];
		int h = m_posY + m_collideBox[3];
		int ox = actor.m_posX + actor.m_collideBox[0];
		int oy = actor.m_posY + actor.m_collideBox[1];
		int ow = actor.m_posX + actor.m_collideBox[2];
		int oh = actor.m_posY + actor.m_collideBox[3];
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_phylayer == actor.m_phylayer && result);
		}
	}

	/**
	 * 与指定Actor进行帧VS帧的碰撞判断
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * @return
	 */
	public boolean frameCollideFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null || actor.m_anim == null
				|| m_anim.m_frames_activeBox.length == 0)
			return false;
		if (m_anim == null || m_anim.m_frames_collideBox.length == 0) {
			return false;
		}
		int x = m_posX + m_anim.m_frames_collideBox[m_frameIndex][0];
		int y = m_posY + m_anim.m_frames_collideBox[m_frameIndex][1];
		int w = m_posX + m_anim.m_frames_collideBox[m_frameIndex][2];
		int h = m_posY + m_anim.m_frames_collideBox[m_frameIndex][3];
		int ox = actor.m_posX
				+ actor.m_anim.m_frames_collideBox[actor.m_frameIndex][0];
		int oy = actor.m_posY
				+ actor.m_anim.m_frames_collideBox[actor.m_frameIndex][1];
		int ow = actor.m_posX
				+ actor.m_anim.m_frames_collideBox[actor.m_frameIndex][2];
		int oh = actor.m_posY
				+ actor.m_anim.m_frames_collideBox[actor.m_frameIndex][3];
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_phylayer == actor.m_phylayer && result);
		}
	}

	/**
	 * 与指定Actor进行帧VSActor的碰撞判断
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * @return
	 */
	public boolean frameCollideActor(Actor actor, boolean ignoreLayer)
	{
		if (actor == null)
			return false;
		if (m_anim == null || m_anim.m_frames_collideBox.length == 0) {
			return false;
		}
		int x = m_posX + m_anim.m_frames_collideBox[m_frameIndex][0];
		int y = m_posY + m_anim.m_frames_collideBox[m_frameIndex][1];
		int w = m_posX + m_anim.m_frames_collideBox[m_frameIndex][2];
		int h = m_posY + m_anim.m_frames_collideBox[m_frameIndex][3];
		int ox = actor.m_posX + actor.m_collideBox[0];
		int oy = actor.m_posY + actor.m_collideBox[1];
		int ow = actor.m_posX + actor.m_collideBox[2];
		int oh = actor.m_posY + actor.m_collideBox[3];
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_phylayer == actor.m_phylayer && result);
		}
	}

	/**
	 * 与指定Actor进行ActorVS帧的碰撞判断
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * @return
	 */
	public boolean actorCollideFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null || actor.m_anim == null
				|| actor.m_anim.m_frames_collideBox == null)
			return false;
		if (m_anim == null || m_anim.m_frames_collideBox.length == 0) {
			return false;
		}
		int x = m_posX + m_collideBox[0];
		int y = m_posY + m_collideBox[1];
		int w = m_posX + m_collideBox[2];
		int h = m_posY + m_collideBox[3];
		int ox = actor.m_posX
				+ actor.m_anim.m_frames_activeBox[actor.m_frameIndex][0];
		int oy = actor.m_posY
				+ actor.m_anim.m_frames_activeBox[actor.m_frameIndex][1];
		int ow = actor.m_posX
				+ actor.m_anim.m_frames_activeBox[actor.m_frameIndex][2];
		int oh = actor.m_posY
				+ actor.m_anim.m_frames_activeBox[actor.m_frameIndex][3];
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_phylayer == actor.m_phylayer && result);
		}
	}

	/**
	 * 判断与指定的actor下一帧是否碰撞
	 * 
	 * @param actor
	 * Actor
	 * @param ignoreLayer
	 * boolean
	 * @return boolean
	 */
	public boolean collideNextFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x = m_posX + m_collideBox[0] + (m_vX >> ACTOR_FRACTION_BIT);
		int y = m_posY + m_collideBox[1] + (m_vY >> ACTOR_FRACTION_BIT);
		int w = m_posX + m_collideBox[2] + (m_vX >> ACTOR_FRACTION_BIT);
		int h = m_posY + m_collideBox[3] + (m_vY >> ACTOR_FRACTION_BIT);
		int ox = actor.m_posX + actor.m_collideBox[0]
				+ (actor.m_vX >> ACTOR_FRACTION_BIT);
		int oy = actor.m_posY + actor.m_collideBox[1]
				+ (actor.m_vY >> ACTOR_FRACTION_BIT);
		int ow = actor.m_posX + actor.m_collideBox[2]
				+ (actor.m_vX >> ACTOR_FRACTION_BIT);
		int oh = actor.m_posY + actor.m_collideBox[3]
				+ (actor.m_vY >> ACTOR_FRACTION_BIT);
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_phylayer == actor.m_phylayer && result);
		}
	}

	/**
	 * 判断与指定的actor上一帧是否碰撞
	 * 
	 * @param actor
	 * Actor
	 * @param ignoreLayer
	 * boolean
	 * @return boolean
	 */
	public boolean collidePreFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x = m_prevPosX + m_collideBox[0];
		int y = m_prevPosY + m_collideBox[1];
		int w = m_prevPosX + m_collideBox[2];
		int h = m_prevPosY + m_collideBox[3];
		int ox = actor.m_prevPosX + actor.m_collideBox[0];
		int oy = actor.m_prevPosY + actor.m_collideBox[1];
		int ow = actor.m_prevPosX + actor.m_collideBox[2];
		int oh = actor.m_prevPosY + actor.m_collideBox[3];
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_phylayer == actor.m_phylayer && result);
		}
	}

	/**
	 * 判断当前帧自身和指定Actor的ActiveBox是否相交
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * 是否忽略元件层
	 * @return
	 */
	public boolean activeCollide(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x = m_posX + m_activeBox[0];
		int y = m_posY + m_activeBox[1];
		int w = m_posX + m_activeBox[2];
		int h = m_posY + m_activeBox[3];
		int ox = actor.m_posX + actor.m_activeBox[0];
		int oy = actor.m_posY + actor.m_activeBox[1];
		int ow = actor.m_posX + actor.m_activeBox[2];
		int oh = actor.m_posY + actor.m_activeBox[3];
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 判断上一帧自身和指定Actor的ActiveBox是否相交
	 * 
	 * @param actor
	 * Actor
	 * @param ignoreLayer
	 * boolean
	 * @return boolean
	 */
	public boolean activeCollidePreFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x = m_prevPosX + m_activeBox[0];
		int y = m_prevPosY + m_activeBox[1];
		int w = m_prevPosX + m_activeBox[2];
		int h = m_prevPosY + m_activeBox[3];
		int ox = actor.m_prevPosX + actor.m_activeBox[0];
		int oy = actor.m_prevPosY + actor.m_activeBox[1];
		int ow = actor.m_prevPosX + actor.m_activeBox[2];
		int oh = actor.m_prevPosY + actor.m_activeBox[3];
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_phylayer == actor.m_phylayer && result);
		}
	}

	/**
	 * 判断当前帧自身activeBox和指定Actor的collideBox是否相交
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * 是否忽略元件层
	 * @return
	 */
	public boolean aToCCollide(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x = m_posX + m_activeBox[0];
		int y = m_posY + m_activeBox[1];
		int w = m_posX + m_activeBox[2];
		int h = m_posY + m_activeBox[3];
		int ox = actor.m_posX + actor.m_collideBox[0];
		int oy = actor.m_posY + actor.m_collideBox[1];
		int ow = actor.m_posX + actor.m_collideBox[2];
		int oh = actor.m_posY + actor.m_collideBox[3];
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 判断上一帧自身activeBox和指定Actor的collideBox是否相交
	 * 
	 * @param actor
	 * Actor
	 * @param ignoreLayer
	 * boolean
	 * @return boolean
	 */
	public boolean aToCCollidePreFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x = m_prevPosX + m_activeBox[0];
		int y = m_prevPosY + m_activeBox[1];
		int w = m_prevPosX + m_activeBox[2];
		int h = m_prevPosY + m_activeBox[3];
		int ox = actor.m_prevPosX + actor.m_collideBox[0];
		int oy = actor.m_prevPosY + actor.m_collideBox[1];
		int ow = actor.m_prevPosX + actor.m_collideBox[2];
		int oh = actor.m_prevPosY + actor.m_collideBox[3];
		boolean result = Tools.collide(x, y, w, h, ox, oy, ow, oh);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_phylayer == actor.m_phylayer && result);
		}
	}

	// 以上代码为元件的通用部分，请不要随意修改，尽可能统一进行
	// ///////////////////////////////////////////////////////////

	// ///////////////////////////////////////////////////////////
	// 元件自定义代码区开始……
	/**
	 * 初始化元件，在元件载入数据后调用，进行一些预设工作 此方法根据游戏不同将进行扩展
	 */
	public void initActor()
	{

	}

	/**
	 * 公共的逻辑
	 */
	public void ai_COMMON()
	{

	}

	/**
	 * 元件AI行为，不同的游戏需要实现此方法来实现不同的元件行为
	 */
	public void doAI()
	{
		ai_COMMON();
	}

	protected final static int COL_TOP = 1;
	protected final static int COL_BOTTOM = 2;
	protected final static int COL_LEFT = 4;
	protected final static int COL_RIGHT = 8;

	/**
	 * 检查元件位置的物理层是否可以放置，并且将元件放置
	 */
	public boolean landingActor()
	{
		int phy = getCurTilePhy(m_posX, m_posY);
		if ((phy > 0 && phy < 5) || phy == 9 || phy == 10 || phy == 11) {
			if (phy > 0 && phy < 5) {
				m_layer = phy;
			}
			m_phylayer = phy;
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * 获得特定位置的物理层数值
	 * 
	 * @param posX
	 * int
	 * @param posY
	 * int
	 * @return int
	 */
	public int getCurTilePhy(int posX, int posY)
	{
		if (m_scene.m_bg == null || m_scene.m_bg.m_phylayer == null) { // 如果没有物理层数据
			return -1;
		}
		int curX = posX / m_scene.m_bg.m_tileW;
		int curY = posY / m_scene.m_bg.m_tileH;
		if (curX >= m_scene.m_bg.m_columns || curY >= m_scene.m_bg.m_rows
				|| curX < 0 || curY < 0)
			return -1;
		return m_scene.m_bg.m_phylayer.m_phydata[curY * m_scene.m_bg.m_columns
				+ curX];
	}

	public int getCurPhy(int tileX, int tileY)
	{
		if (m_scene.m_bg == null || m_scene.m_bg.m_phylayer == null) { // 如果没有物理层数据
			return -1;
		}
		if (tileX >= m_scene.m_bg.m_columns || tileY >= m_scene.m_bg.m_rows
				|| tileX < 0 || tileY < 0)
			return -1;
		return m_scene.m_bg.m_phylayer.m_phydata[tileY * m_scene.m_bg.m_columns
				+ tileX];
	}

	/**
	 * 
	 * @param posX
	 * int
	 * @param posY
	 * int
	 * @param isDown
	 * boolean
	 * @return int
	 */
	public int getNextTile(int posX, int posY, boolean toRight)
	{
		int curX = posX / m_scene.m_bg.m_tileW;
		int curY = posY / m_scene.m_bg.m_tileH;
		if (toRight) {
			return m_scene.m_bg.m_phylayer.m_phydata[curY
					* m_scene.m_bg.m_columns + curX + 1];
		}
		else {
			return m_scene.m_bg.m_phylayer.m_phydata[curY
					* m_scene.m_bg.m_columns + curX - 1];
		}
	}

	/**
	 * 将Actor定位到它所在tile的中心
	 */
	public void locateActor()
	{
		m_posX = m_posX / m_scene.m_bg.m_tileW * m_scene.m_bg.m_tileW
				+ m_scene.m_bg.m_tileW / 2;
		m_posY = m_posY / m_scene.m_bg.m_tileH * m_scene.m_bg.m_tileH
				+ m_scene.m_bg.m_tileH / 2;
	}

	/**
	 * 元件与背景层物理层的碰撞逻辑处理
	 */
	public void bgCollide()
	{
		if (m_collideBox == null) {
			return;
		}
		int collide = 0;
		int collideX = m_posX + m_collideBox[0];
		int collideWidth = m_posX + m_collideBox[2];
		int collideY = m_posY + m_collideBox[1];
		int collideHeight = m_posY + m_collideBox[3];
		int startTileX = collideX / m_scene.m_bg.m_tileW;
		int startTileY = collideY / m_scene.m_bg.m_tileH;
		int endTileX = collideWidth / m_scene.m_bg.m_tileW;
		int endTileY = collideHeight / m_scene.m_bg.m_tileH;
		int i, j;
		startTileX = Tools.limit(startTileX, 0, m_scene.m_bg.m_columns - 1);
		endTileX = Tools.limit(endTileX, 0, m_scene.m_bg.m_columns - 1);
		startTileY = Tools.limit(startTileY, 0, m_scene.m_bg.m_rows - 1);
		endTileY = Tools.limit(endTileY, 0, m_scene.m_bg.m_rows - 1);

		collide = 0;
		for (i = startTileX; i <= endTileX; i++) {
			for (j = startTileY; j <= endTileY; j++) {
				int collideNum = m_scene.m_bg.m_phylayer.m_phydata[j
						* m_scene.m_bg.m_columns + i];
				if (collideNum == Background.PHY_BLOCK) {
					if (i == startTileX && m_vX < 0) {
						m_posX = (i + 1) * m_scene.m_bg.m_tileW
								- m_collideBox[0];
						collide |= COL_LEFT;
					}
					if (i == endTileX && m_vX > 0) {
						m_posX = i * m_scene.m_bg.m_tileW - m_collideBox[2] - 1;
						collide |= COL_RIGHT;
					}
					if (j == startTileY && m_vY < 0) {
						m_posY = (j + 1) * m_scene.m_bg.m_tileH
								- m_collideBox[1];
						collide |= COL_TOP;
					}
					if (j == endTileY && m_vY > 0) {
						m_posY = j * m_scene.m_bg.m_tileH - m_collideBox[3] - 1;
						collide |= COL_BOTTOM;
					}
				}
			}
		}

		int phy = -1;
		int phy2 = -1;
		if ((collide & COL_LEFT) != 0) { // 左边碰撞
			phy = m_scene.m_bg.m_phylayer.m_phydata[startTileY
					* m_scene.m_bg.m_columns + startTileX];
			phy2 = m_scene.m_bg.m_phylayer.m_phydata[Tools.limit(
					startTileY - 1, 0, m_scene.m_bg.m_rows - 1)
					* m_scene.m_bg.m_columns + startTileX];
			if (phy == -1 || phy2 == -1) {
				if (phy == -1 && phy2 != -1) {
					m_posY -= m_velocity >> ACTOR_FRACTION_BIT;
					m_posY = Math.max((startTileY + 1) * m_scene.m_bg.m_tileH
							- m_collideBox[3] - 1, m_posY);
				}
				else {
					m_posY -= m_velocity >> ACTOR_FRACTION_BIT;
				}
				return;
			}
			phy = m_scene.m_bg.m_phylayer.m_phydata[endTileY
					* m_scene.m_bg.m_columns + startTileX];
			phy2 = m_scene.m_bg.m_phylayer.m_phydata[Tools.limit(endTileY + 1,
					0, m_scene.m_bg.m_rows - 1)
					* m_scene.m_bg.m_columns
					+ startTileX];
			if (phy == -1 || phy2 == -1) {
				if (phy == -1 && phy2 != -1) {
					m_posY += m_velocity >> ACTOR_FRACTION_BIT;
					m_posY = Math.min(endTileY * m_scene.m_bg.m_tileH
							- m_collideBox[1], m_posY);
				}
				else {
					m_posY += m_velocity >> ACTOR_FRACTION_BIT;
				}
				return;
			}
		}

		if ((collide & COL_RIGHT) != 0) { // 右边碰撞
			phy = m_scene.m_bg.m_phylayer.m_phydata[startTileY
					* m_scene.m_bg.m_columns + endTileX];
			phy2 = m_scene.m_bg.m_phylayer.m_phydata[Tools.limit(
					startTileY - 1, 0, m_scene.m_bg.m_rows - 1)
					* m_scene.m_bg.m_columns + endTileX];
			if (phy == -1 || phy2 == -1) {
				if (phy == -1 && phy2 != -1) {
					m_posY -= m_velocity >> ACTOR_FRACTION_BIT;
					m_posY = Math.max((startTileY + 1) * m_scene.m_bg.m_tileH
							- m_collideBox[3] - 1, m_posY);
				}
				else {
					m_posY -= m_velocity >> ACTOR_FRACTION_BIT;
				}
				return;
			}
			phy = m_scene.m_bg.m_phylayer.m_phydata[endTileY
					* m_scene.m_bg.m_columns + endTileX];
			phy2 = m_scene.m_bg.m_phylayer.m_phydata[Tools.limit(endTileY + 1,
					0, m_scene.m_bg.m_rows - 1)
					* m_scene.m_bg.m_columns
					+ endTileX];
			if (phy == -1 || phy2 == -1) {
				if (phy == -1 && phy2 != -1) {
					m_posY += m_velocity >> ACTOR_FRACTION_BIT;
					m_posY = Math.min(endTileY * m_scene.m_bg.m_tileH
							- m_collideBox[1], m_posY);
				}
				else {
					m_posY += m_velocity >> ACTOR_FRACTION_BIT;
				}
				return;
			}
		}

		if ((collide & COL_TOP) != 0) { // 上边碰撞
			phy = m_scene.m_bg.m_phylayer.m_phydata[startTileY
					* m_scene.m_bg.m_columns + startTileX];
			phy2 = m_scene.m_bg.m_phylayer.m_phydata[startTileY
					* m_scene.m_bg.m_columns
					+ Tools.limit(startTileX - 1, 0, m_scene.m_bg.m_columns - 1)];
			if (phy == -1 || phy2 == -1) {
				if (phy == -1 && phy2 != -1) {
					m_posX -= m_velocity >> ACTOR_FRACTION_BIT;
					m_posX = Math.max((startTileX + 1) * m_scene.m_bg.m_tileW
							- m_collideBox[2] - 1, m_posX);
				}
				else {
					m_posX -= m_velocity >> ACTOR_FRACTION_BIT;
				}
				m_flipX = true;
				return;
			}
			phy = m_scene.m_bg.m_phylayer.m_phydata[startTileY
					* m_scene.m_bg.m_columns + endTileX];
			phy2 = m_scene.m_bg.m_phylayer.m_phydata[startTileY
					* m_scene.m_bg.m_columns
					+ Tools.limit(endTileX + 1, 0, m_scene.m_bg.m_columns - 1)];
			if (phy == -1 || phy2 == -1) {
				if (phy == -1 && phy2 != -1) {
					m_posX += m_velocity >> ACTOR_FRACTION_BIT;
					m_posX = Math.min(endTileX * m_scene.m_bg.m_tileW
							- m_collideBox[0], m_posX);
				}
				else {
					m_posX += m_velocity >> ACTOR_FRACTION_BIT;
				}
				m_flipX = false;
				return;
			}
		}

		if ((collide & COL_BOTTOM) != 0) { // 下边碰撞
			phy = m_scene.m_bg.m_phylayer.m_phydata[endTileY
					* m_scene.m_bg.m_columns + startTileX];
			phy2 = m_scene.m_bg.m_phylayer.m_phydata[endTileY
					* m_scene.m_bg.m_columns
					+ Tools.limit(startTileX - 1, 0, m_scene.m_bg.m_columns - 1)];
			if (phy == -1 || phy2 == -1) {
				if (phy == -1 && phy2 != -1) {
					m_posX -= m_velocity >> ACTOR_FRACTION_BIT;
					m_posX = Math.max((startTileX + 1) * m_scene.m_bg.m_tileW
							- m_collideBox[2] - 1, m_posX);
				}
				else {
					m_posX -= m_velocity >> ACTOR_FRACTION_BIT;
				}
				m_flipX = true;
				return;
			}
			phy = m_scene.m_bg.m_phylayer.m_phydata[endTileY
					* m_scene.m_bg.m_columns + endTileX];
			phy2 = m_scene.m_bg.m_phylayer.m_phydata[endTileY
					* m_scene.m_bg.m_columns
					+ Tools.limit(endTileX + 1, 0, m_scene.m_bg.m_columns - 1)];
			if (phy == -1 || phy2 == -1) {
				if (phy == -1 && phy2 != -1) {
					m_posX += m_velocity >> ACTOR_FRACTION_BIT;
					m_posX = Math.min(endTileX * m_scene.m_bg.m_tileW
							- m_collideBox[0], m_posX);
				}
				else {
					m_posX += m_velocity >> ACTOR_FRACTION_BIT;
				}
				m_flipX = false;
				return;
			}
		}
	}

	/**
	 * flag标记中ACTOR_FLAG_SELF是否被勾选
	 * 
	 * @return
	 */
	public boolean isSelfActor()
	{
		return (m_flag & Actor.ACTOR_FLAG_SELF) != 0;
	}

	/**
	 * 设置标记位
	 * 
	 * @param index
	 * int
	 * @param status
	 * boolean
	 */
	public void setFlag(int index, boolean status)
	{
		if (index == 0) {
			m_flipX = status;
		}
		else if (index == 1) {
			m_flipY = status;
		}
		else if (index == 2) {
			setEnabled(status);
		}
		else if (index == 3) {
			setVisible(status);
		}
		else {
			if (status) {
				m_flag |= 1 << index;
			}
			else {
				m_flag &= ~(1 << index);
			}
		}
	}

	/**
	 * 获得标记位状态
	 * 
	 * @param index
	 * int
	 * @return boolean
	 */
	public boolean getFlag(int index)
	{
		if (index == 0) {
			return m_flipX;
		}
		else if (index == 1) {
			return m_flipY;
		}
		else if (index == 2) {
			return m_enabled;
		}
		else if (index == 3) {
			return m_visible;
		}
		return (m_flag & 1 << index) != 0;
	}

	/**
	 * 获得activebox的中心坐标X
	 * 
	 * @return
	 */
	public int getActiveBoxCenterX()
	{
		return m_posX + (m_activeBox[2] + m_activeBox[0]) / 2;
	}

	/**
	 * 获得activebox的中心坐标Y
	 * 
	 * @return
	 */
	public int getActiveBoxCenterY()
	{
		return m_posY + (m_activeBox[3] + m_activeBox[1]) / 2;
	}

	/**
	 * 获得collidebox的中心坐标X
	 * 
	 * @return
	 */
	public int getCollideBoxCenterX()
	{
		return m_posX + (m_collideBox[2] + m_collideBox[0]) / 2;
	}

	/**
	 * 获得collidebox的中心坐标Y
	 * 
	 * @return
	 */
	public int getCollideBoxCenterY()
	{
		return m_posY + (m_collideBox[3] + m_collideBox[1]) / 2;
	}

	// 元件自定义代码区结束
	// ///////////////////////////////////////////////////////////

	public static final int ACTOR_FLAG_CG = 1 << 4; // 是否过场动画元件
	public static final int ACTOR_FLAG_EVENT = 1 << 5; // 是否触发事件
	public static final int ACTOR_FLAG_SELF = 1 << 6; // 是否玩家控制角色

	public static final int ACTOR_FRACTION_BIT = 10;

	public void loadAnimations(Resource res) throws IOException
	{
		m_anims = new Animation[m_refsResHead.length];
		for (int i = 0; i < m_anims.length; i++) {
			m_anims[i] = (Animation) (ResourceManager.getInstance()
					.loadResource(res, m_refsResHead[i]));
		}
		if (m_anims.length > 0) {
			m_anim = m_anims[0];
		}
	}

	public void read(Resource res, DataInputStream dis) throws IOException
	{
		readHeader(res, dis);
		loadAnimations(res);
		m_flag = dis.readInt();
		m_flipX = (m_flag & 0x01) != 0;
		m_flipY = (m_flag & 0x02) != 0;
		m_enabled = (m_flag & 0x04) != 0;
		m_visible = (m_flag & 0x08) != 0;
		int action = dis.readByte();
		if (action < 0) {
			action += 256;
		}
		setAction(action, true);
		m_collideBox[0] = dis.readShort();
		m_collideBox[1] = dis.readShort();
		m_collideBox[2] = (short) (dis.readShort() + m_collideBox[0]);
		m_collideBox[3] = (short) (dis.readShort() + m_collideBox[1]);
		m_activeBox[0] = dis.readShort();
		m_activeBox[1] = dis.readShort();
		m_activeBox[2] = (short) (dis.readShort() + m_activeBox[0]);
		m_activeBox[3] = (short) (dis.readShort() + m_activeBox[1]);
		int temp_parameterCount = dis.readByte();
		if (temp_parameterCount != 0) {
			m_parameters = new short[temp_parameterCount];
			for (int j = 0; j < temp_parameterCount; j++) {
				m_parameters[j] = dis.readShort();
			}
		}
		checkLoadFinished();
	}

	public void checkLoadFinished()
	{
		if (m_anims == null) {
			m_loadFinished = false;
			return;
		}
		for (int i = 0; i < m_anims.length; i++) {
			if (m_anims[i] == null || m_anims[i].m_resHead.equals(m_refsResHead[i])) {
				m_loadFinished = false;
				return;
			}
		}
		m_loadFinished = true;
	}

	public synchronized void dispose()
	{
		disposeHeader();
		if (m_anims != null) {
			for (int i = 0; i < m_anims.length; i++) {
				if (m_anims[i] != null) {
					m_anims[i] = null;
				}
			}
			m_anims = null;
		}
		m_anim = null;
		m_collideBox = null;
		m_activeBox = null;
		m_parameters = null;
	}

	/**
	 * 销毁该Actor，包括其动画文件
	 */
	public synchronized void destroy()
	{

		if (m_anims != null) {
			for (int i = 0; i < m_anims.length; i++) {
				animsDestroy(m_anims[i], m_refsResHead[i]);
			}
			m_anims = null;
		}
		disposeHeader();
		m_anim = null;
		m_collideBox = null;
		m_activeBox = null;
		m_parameters = null;
	}

	/**
	 * 删除动画
	 * @param type 0 默认清理所有 其他值为指定
	 * */
	public void animsDestroy(Animation _anim, ResHead _resHead)
	{
		if (_anim != null) {
			boolean isUsing = false;
			if (m_resource != null) {
				isUsing = m_resource.isUseing(this, _anim.m_resHead);
			}
			if (!isUsing && m_scene != null) {
				Actor actor;
				ii: for (int ii = 0; ii < m_scene.m_dynamic_actors.size(); ii++) {
					actor = (Actor) m_scene.m_dynamic_actors.elementAt(ii);
					if (actor.isContainHeader(_anim.m_resHead) && actor != this) {
						isUsing = true;
						break ii;
					}
				}
			}

			// 从res中移除依赖的Animation,并销毁它
			if (!isUsing) {
				if (_anim != null && _anim.m_resHead.equals(_resHead)) {
					_anim.destroy();
					m_resource.remove(_anim.m_resHead);
					Tools.debugPrintln("isUsing=" + isUsing);
				}
			}
			_anim = null;
		}
	}

//	public synchronized void destroy()
//	{
//     
//		if (m_anims != null) {
//			i: for (int i = 0; i < m_anims.length; i++) {
//				if (m_anims[i] != null) {
//					boolean isUsing = false;
//					if (m_resource != null) {
//						isUsing = m_resource.isUseing(this, m_anims[i].m_resHead);
//					}
//					if (!isUsing && m_scene != null) {
//						Actor actor;
//						ii: for (int ii = 0; ii < m_scene.m_dynamic_actors.size(); ii++) {
//							actor = (Actor) m_scene.m_dynamic_actors.elementAt(ii);
//							if ( actor.isContainHeader(m_anims[i].m_resHead) && actor != this) {
//								isUsing = true;
//								break ii;
//							}
//						}
//					}
//
//					// 从res中移除依赖的Animation,并销毁它
//					if (!isUsing ) {
//						if( m_anims[i] != null && m_anims[i].m_resHead.equals(m_refsResHead[i]) ){
//						    m_anims[i].destroy();
//					 	    m_resource.remove(m_anims[i].m_resHead);
//						}
//					}
//					m_anims[i] = null;
//				}
//			}
//			m_anims = null;
//		}
//		disposeHeader();
//		m_anim = null;
//		m_collideBox = null;
//		m_activeBox = null;
//		m_parameters = null;
//	}

	public synchronized void updateComponent(ResHead head,
			Serializeable component)
	{
		if (m_loadFinished)
			return;
		if (head.m_type != ObjectAdapter.RESOURCE_TYPE_ANIM)
			return;
		if (m_refsResHead == null || m_refsResHead.length == 0)
			return;
		for (int i = 0; i < m_refsResHead.length; i++) {
			if (m_refsResHead[i] != null && m_refsResHead[i].equals(head)) {
				m_anims[i] = (Animation) component;
				checkLoadFinished();
			}
		}
		m_anim = m_anims[0];
	}

	public boolean isContainHeader(ResHead head)
	{
		if (head == null)
			return false;
		if (head.m_type != ObjectAdapter.RESOURCE_TYPE_ANIM)
			return false;
		if (m_refsResHead == null || m_refsResHead.length == 0)
			return false;
		for (int i = 0; i < m_refsResHead.length; i++) {
			if (m_refsResHead[i] != null && m_refsResHead[i].equals(head)) {
				return true;
			}
		}
		return false;
	}

	public static final int ACTION_NONE = Byte.MIN_VALUE;
}
